﻿using System;
using System.Collections;
using System.Collections.Generic;
using Me.Prettyprint.Cassandra.Connection;
using Me.Prettyprint.Cassandra.Serializers;
using Me.Prettyprint.Cassandra.Service;
using Me.Prettyprint.Cassandra.Utils;
using Me.Prettyprint.Hector.Api;
using Me.Prettyprint.Hector.Api.Beans;
using Me.Prettyprint.Hector.Api.Query;
using Org.Apache.Cassandra.Thrift;

namespace Me.Prettyprint.Cassandra.Model
{
    public class MultigetCountQuery<K,N> : Query<Dictionary<byte[],int>>
    {
        public MultigetCountQuery(Keyspace ks, Serializer<K> keySerializer,
            Serializer<int> nameSerializer)
        {
            if (ks == null || keySerializer == null || nameSerializer == null)
            {
                throw new NullReferenceException("keyspace, key serializer and name serializer cannot be null");
            }
            Keyspace = (ExecutingKeyspace)ks;
            KeySerializer = keySerializer;
            SlicePredicate = new HSlicePredicate<int>(nameSerializer);
        }
        public ExecutingKeyspace Keyspace
        {
            get;set;
        }
        protected Serializer<K> KeySerializer
        {
            get;set;
        }
        protected string ColumnFamily
        {
            get;set;
        }
        protected List<K> Keys
        {
            get;set;
        }
        protected HSlicePredicate<int> SlicePredicate
        {
            get;set;
        }
        public MultigetCountQuery<K,N> SetKeys(List<K> keys)
        {
            Keys = keys;
            return this;
        }
        public MultigetCountQuery<K,N> SetColumnFamily(string cf)
        {
            ColumnFamily = cf;
            return this;
        }
        public MultigetCountQuery<K,N> SetColumnNames(List<int> columnNames)
        {
            SlicePredicate.SetColumnNames(columnNames);
            return this;
        }
        public MultigetCountQuery<K,N> SetRange(int start, int finish, int count)
        {
            SlicePredicate.SetRange(start, finish, false, count);
            return this;
        }
        public override string ToString()
        {
            return string.Format("MultigetCountQuery({0}) on cf: {1} with keys: {2}", SlicePredicate.ToString(),ColumnFamily,Keys.ToString());
        }
        public QueryResult<Dictionary<byte[], int>> Execute()
        {
            if (Keys == null || ColumnFamily == null)
            {
                throw new NullReferenceException("keys and Column family cannot be null");
            }
            Func<KeyspaceService, Dictionary<byte[], int>> x = (KeyspaceService ks) =>
            {
                ColumnParent columnParent = new ColumnParent(ColumnFamily);
                Dictionary<byte[], int> counts = ks.MultigetCount(KeySerializer.ToBytesList(Keys), columnParent, SlicePredicate.ToThrift());
                return counts;
            };
            return new QueryResultImpl<Dictionary<byte[], int>>(
                Keyspace.DoExecute(new KeyspaceOperationCallbackBuilder<Dictionary<byte[], int>>(x)), this);
        }
    }
}
